package com.sff.manager {
	import com.sff.core.sff_internal;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class ResizeManager extends EventDispatcher {

		internal var _inputTransform 	: ModuleTransform;		
		internal var _outputTransform 	: ModuleTransform;
		
		internal var _input 			: Rectangle;
		internal var _position 			: Rectangle;
		
		public function ResizeManager() {
			super( );
			
			_input = new Rectangle();
			_position = new Rectangle();
			
			_inputTransform = new ModuleTransform();
			_outputTransform = new ModuleTransform();
		}

		//public function update() : void {
		//	sff_internal::setTransform(_input);
		//}

		public function set x (x : Number)  : void {
			_inputTransform.x = x;
			
			sff_internal::setTransform(_input);
		}
		
		public function get x() : Number {
			return _position.x;
		}
		
		public function set y (y : Number)  : void {
			_inputTransform.y = y;
			
			sff_internal::setTransform(_input);
		}
		
		public function get y() : Number {
			return _position.y;
		}
		
		public function set width (w : int)  : void {
			_inputTransform.width = w;
			
			sff_internal::setTransform(_input);
		}
		
		public function get width ()  : int {
			return _position.width;
		}
		
		public function set height (h : int)  : void {
			_inputTransform.height = h;
			
			sff_internal::setTransform(_input);
		}
		
		public function get height ()  : int {
			return _position.height;
		}
		
		public function setSize( w : int, h : int ) : void {
			_inputTransform.width = w;
			_inputTransform.height = h;
			
			sff_internal::setTransform(_input);
		}

		sff_internal function setTransform(rect : Rectangle ) : void {
			
			if( !rect ) {
				return;
			}
			_input = rect;
			
			var newPosition : Rectangle = _inputTransform.add( rect.clone() );
			if( _position.equals( newPosition) ) {
				return;
			}			
			_position = newPosition;
			
			change();
		}
		
		sff_internal function getTransform( ) : Rectangle {
			var out : Rectangle = _position.clone();
			out.x = out.y = 0;
			out = _outputTransform.add(out )
			
			return out;
		}
				
		public function change() : void {
			dispatchEvent( new Event( Event.RESIZE  ) );
		}
		
		//public function get transform() : ModuleTransform {
		//	return _inputTransform;
		//}
		
		//public function set transform(inputTransform : ModuleTransform) : void {
		//	_inputTransform = inputTransform;
		//	sff_internal::setTransform(_input);
		//}
		
		//public function get outputTransform() : ModuleTransform {
		//	return _outputTransform;
		//}
		
		//public function set outputTransform(outputTransform : ModuleTransform) : void {
		//	_outputTransform = outputTransform;
		//}
		
		public function clone() : ResizeManager {
			var rm : ResizeManager = new ResizeManager();
			
			rm._inputTransform = _inputTransform.clone();
			rm._outputTransform = _outputTransform.clone();
			
			rm._position = _position.clone();			
			rm.sff_internal::setTransform(_input.clone() );
			
			return rm;
		}
	}
}


import flash.geom.Point;
import flash.geom.Rectangle;	
	
class ModuleTransform {
	
	//x, y
	internal var _position 	: Point;
	
	//width, height
	internal var _size		: Point;
	
	//offsetX, offsetY
	internal var _offset	: Point;
		
	public function ModuleTransform() {				
		_position = new Point();
		_offset = new Point();
	}
	
	public function set x ( value : Number ) : void {
		_position.x = value;
	}
	
	public function set y ( value : Number ) : void {
		_position.y = value;
	}
	
	public function set width ( value : Number ) : void {
		if (!_size) {
			_size = new Point();
		}
		_size.x = value;
	}
	
	public function set height ( value : Number ) : void {
		if (!_size) {
			_size = new Point();
		}
		_size.y = value;
	}
	
	public function set offsetX(value:Number):void {
		_offset.x = value;
	}
	
	public function set offsetY(value:Number):void {
		_offset.y = value;
	}
	
	public function add(rect : Rectangle ): Rectangle {
		
		if (_size) {
			rect.width = _size.x;			
			rect.height = _size.y;			
		}
		
		rect.width += _offset.x;
		rect.height += _offset.y;
		
		rect.x += _position.x;
		rect.y += _position.y;
		
		return rect;
	}
	
	public function clone() : ModuleTransform {
		var mt : ModuleTransform = new ModuleTransform(); 
		
		//copy position
		mt._position = _position;
		
		//copy size
		if (_size) {
			mt._size = _size;									
		}
		
		//copy offset
		mt._offset = _offset;
		
		return mt;
	}
}